<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        class="buttons"
        @click="handleCreate"
      >
        添加
      </el-button>
      <el-input
        v-if="false"
        v-model="listQuery.keyword"
        placeholder="请输入关键词"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button v-if="false" class="buttons" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      :data="list"
      :header-cell-style="{background:'#ebebeb',height:'#54px'}"
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column
        label="ID"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="收件人"
        prop="realname"
        align="center"
        sortable="custom"
      >
        <template slot-scope="{row}">
          <span>{{ row.realname }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="联系电话"
        prop="mobile"
        align="center"
        sortable="custom"
      >
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="地区"
        prop="name"
        align="center"
        sortable="custom"
      >
        <template slot-scope="{row}">
          <span>{{ row.areaData }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="详细地址"
        prop="name"
        align="center"
        sortable="custom"
      >
        <template slot-scope="{row}">
          <span>{{ row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="false"
        label="添加时间"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.addtime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="是否默认收货地址"
        min-width="150px"
      >
        <template slot-scope="{row}">
          <el-tag>{{ row.selected===1?'是':'否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row,$index}">
          <el-button
            type="primary"
            size="mini"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="删除后不可以恢复，确定删除吗？"
            @onConfirm="handleDelete(row,$index)"
          >
            <el-button
              slot="reference"
              size="mini"
              type="danger"
            >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑、 添加弹框表单 -->
    <areaDialog :editor-btn="editorBtn" :type="1" handletitle="地址管理" :rule-form="ruleForm" @dialogVisibleEvent="dialogBtn" />
  </div>
</template>

<script>
import { fetchList, del, add, rows } from '@/api/address'
import { notifySuccess } from '@/utils/message'
import areaDialog from '@/components/Address/areaDialog'

export default {
  name: 'AttachmentIndex',
  components: { areaDialog },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listQuery: {
        enterprise_id: 100009,
        id: null,
        page: 1,
        per_page: 15,
        importance: null,
        keyword: null,
        type: null,
        sort: 'id',
        order: 'desc',
        filter: [],
        op: []
      },
      importanceOptions: [1, 2, 3],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      ruleForm: {
        enterprise_id: 1000,
        id: 0,
        area_id: null,
        realname: null,
        mobile: null,
        address: null,
        isdefault: null,
        post_code: null,
        tel: null
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑数据',
        create: '新增'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        jump_url: [{ required: true, message: '请输入链接地址', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        adverts_id: [{ required: true, message: '请选择广告位置', trigger: 'blur' }]
      },
      cateLists: {},
      downloadLoading: false,
      editorBtn: false // 编辑收货地址
      // editorBtn:false
    }
  },
  created() {
    this.listQuery.enterprise_id = this.$route.query.enterprise_id
    this.ruleForm.enterprise_id = this.$route.query.enterprise_id
    this.getList()
  },
  methods: {
    dialogBtn() {
      this.editorBtn = false
    },
    // 获取列表数据
    getList() {
      var that = this
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        setTimeout(() => {
          that.listLoading = false
        }, 1.5 * 1000)
      })
    },
    // 搜索按钮提交
    searchSumit(e) {
      this.listQuery.filter = e.filter
      this.listQuery.op = e.op
      this.listQuery.page = 1
      this.getList()
    },
    // 搜索查询
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      notifySuccess('', '操作成功')
      row.status = status
    },
    // 排序
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sort = prop
      this.sortByID(order)
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.order = 'ASC'
      } else {
        this.listQuery.order = 'DESC'
      }
      this.handleFilter()
    },
    resetTemp() {
      this.rows = {
        enterprise_id: this.ruleForm.enterprise_id,
        id: 0,
        area_id: null,
        realname: null,
        mobile: null,
        address: null,
        isdefault: null,
        post_code: null,
        tel: null
      }
    },
    handleCreate() {
      this.editorBtn = true
      this.resetTemp()
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add(this.rows).then(res => {
            //  this.list.unshift(this.rows)
            this.dialogFormVisible = false
            notifySuccess(res.msg || '添加成功')
            this.resetTemp()
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.ruleForm.area_id = [0, 0, 0]
      this.dialogStatus = 'update'
      this.editorBtn = true
      rows(row.id, this.listQuery.enterprise_id).then(res => {
        this.ruleForm = res.data
        this.ruleForm.area_id = [res.data.province_id, res.data.city_id, res.data.area_id]
        this.dialogStatus = 'update'
        this.editorBtn = true
      })
    },
    // 删除地址
    handleDelete(row, index) {
      del(row.id, this.listQuery.enterprise_id).then(res => {
        this.list.splice(index, 1)
        notifySuccess(res.msg || '删除成功')
      })
    },
    typeChange(e) {
      console.log(e)
    }
  }
}
</script>
